package com.qingcheng.controller.order;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.entity.TuiHu;
import com.qingcheng.pojo.order.ReturnOrder;
import com.qingcheng.service.order.ReturnOrderService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/returnOrder")
public class ReturnOrderController {

    @Reference
    private ReturnOrderService returnOrderService;

    @GetMapping("/findAll")
    public List<ReturnOrder> findAll(){
        return returnOrderService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<ReturnOrder> findPage(int page, int size){
        return returnOrderService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<ReturnOrder> findList(@RequestBody Map<String,Object> searchMap){
        return returnOrderService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public TuiHu findPage(@RequestBody Map<String,Object> searchMap,Integer page, Integer size){
        //获取登录人
        //默认当前页为0
        if(page==null){
            page=1;
        }
        //默认个数为20
        if(size==null){
            size=20;
        }

        return  returnOrderService.findPage(searchMap,page,size);
    }

    @GetMapping("/findById")
    public ReturnOrder findById(Long id){
        return returnOrderService.findById(id);
    }


    @PostMapping("/add")
    public Result add(@RequestBody ReturnOrder returnOrder){
        returnOrderService.add(returnOrder);
        return new Result();
    }

    @PostMapping("/update")
    public Result update(@RequestBody ReturnOrder returnOrder){
        returnOrderService.update(returnOrder);
        return new Result();
    }

    @GetMapping("/delete")
    public Result delete(Long id){
        returnOrderService.delete(id);
        return new Result();
    }

    @GetMapping("/tuihuo")
    public Result tuihuo(Long id){

//        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        String name ="admin";
        //确认退货
        boolean tuihuo = returnOrderService.tuihuo(id, name);
        if(tuihuo){
           return new Result(0," 退货申请成功");
        }else {
            return new Result(1,"退货失败");
        }

    }

    @GetMapping("/sorry")
    public Result sorry(Long id){
//        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        String name ="admin";
        //拒绝退货
        boolean sorry = returnOrderService.sorry(id, name);
        if(sorry){
            return new Result(0," 拒绝退货成功");
        }else {
            return new Result(1,"拒绝退货失败");
        }
    }
}
